#include<stdio.h>
#include<string.h>
#include <stdlib.h>
FILE *file_open(const char *path,const char *mode);
//关闭文件
int file_close(FILE **fp);

//结构体声明
typedef struct str
{
    int id;
    int sco;
}Elenemet_data;
Elenemet_data hqyj[5]={{1,100},{2,98},{3,97},{4,99},{5,100}};
 
 int main(int argc, char const *argv[])
 {  

    for (size_t i = 0; i < 5; i++)
{
    printf("学号%d\t成绩：%d\n",hqyj[i].id,hqyj[i].sco);
}

    FILE *fp=file_open("h.txt","r+");
    for (size_t i = 0; i < 5; i++)
    {
        fprintf(fp,"学号：%d\t成绩：%d\n",hqyj[i].id,hqyj[i].sco);
    }
    file_close(&fp);
//打开文件
    fp=file_open("h.txt","r+");
    Elenemet_data hz[5];
//读取文件
for (size_t i = 0; i < 5; i++)
{
    fscanf(fp,"学号%d\t成绩：%d\n",&hz[i].id,&hz[i].sco);//读取数据
}
for (size_t i = 0; i < 5; i++)
{
    printf("学号%d\t成绩：%d\n",hz[i].id,hz[i].sco);
}
    file_close(&fp);
    return 0;
 }
 

FILE *file_open(const char *path, const char *mode)
{
    FILE *fp = fopen(path, mode);
    if (NULL == fp)
    {
        printf("文件创建失败\n");
        exit(-1);
    }
    return fp;
}

int file_close(FILE **fp)
{
    fclose(*fp);
    *fp = NULL;
}